import axios from 'axios';
import {htmlFilter} from "../../common";

// Action
const DUBBO_INIT_METHOD_DATA = 'yapi/interface_method/INIT_METHOD_DATA';
const DUBBO_UPDATE_CUR_METHOD_DATA = "yapi/interface_method/UPDATE_CUR_METHOD_DATA";
const DUBBO_CHANGE_INTERFACE_METHOD_DATA = "yapi/interface_method/CHANGE_INTERFACE_METHOD_DATA";
const DUBBO_FETCH_METHOD_DATA = 'yapi/interface_method/FETCH_METHOD_DATA';
const DUBBO_CHANGE_EDIT_STATUS = 'yapi/interface_method/CHANGE_EDIT_STATUS';
const DUBBO_FETCH_METHODBASE_DATA= 'yapi/interface_method/FETCH_METHODBASE_DATA';
const DUBBO_FETCH_METHOD_LIST_MENU = 'yapi/interface_method/FETCH_METHOD_LIST_MENU';
const DUBBO_FETCH_METHOD_LIST = 'yapi/interface_method/FETCH_METHOD_LIST';
const DUBBO_FETCH_DUBBO_INTERFACE_LIST = 'yapi/interface_method/FETCH_DUBBO_INTERFACE_LIST';
const DUBBO_DELETE_METHOD_DATA = 'yapi/interface_method/DELETE_METHOD_DATA';
const DUBBO_DELETE_DUBBO_INTERFACE_DATA = 'yapi/interface_method/DELETE_DUBBO_INTERFACE_DATA';
const DUBBO_UP_METHOD_DATA= 'yapi/interface_method/UP_METHOD_DATA';


// Reducer
const initialState = {
  curMethod: {},
  list: [],
  curdata: {},
  editStatus: false, // 记录编辑页面是否有编辑,
  totalTableList: [],
  infTableList: [],
  count: 0,
  totalCount: 0
};

export default (state = initialState, action) => {
  switch (action.type) {
    case DUBBO_INIT_METHOD_DATA:
      return initialState;
    case DUBBO_UPDATE_CUR_METHOD_DATA: {
      return {
        ...state,
        curMethod: Object.assign({}, state.curMethod, action.updata)
      };
    }
    case DUBBO_FETCH_METHOD_DATA:
      return {
        ...state,
        curdata: action.payload.data.data,
        curMethod: action.payload.data.data.curMethod
      };
    case DUBBO_CHANGE_INTERFACE_METHOD_DATA: {
      return {
        ...state,
        curMethod: action.payload.data.data
      }
    }
    case DUBBO_CHANGE_EDIT_STATUS: {
      return {
        ...state,
        editStatus: action.status
      };
    }
    case DUBBO_FETCH_METHOD_LIST: {
      return {
        ...state,
        totalTableList: (!action.payload.data.data ? [] : action.payload.data.data.list),
        totalCount: (!action.payload.data.data ? 0 : action.payload.data.data.count)
        // totalTableList: action.payload.data.data.list,
        // totalCount: action.payload.data.data.count
      };
    }
    case DUBBO_FETCH_METHOD_LIST_MENU: {
      return {
        ...state,
        list: action.payload.data.data
      };
    }
    case DUBBO_FETCH_DUBBO_INTERFACE_LIST: {
      return {
        ...state,
        infTableList: action.payload.data.data.list,
        count: action.payload.data.data.count
      };
    }
    default:
      return state;
  }
};

export function initMethod() {
  return {
    type: DUBBO_INIT_METHOD_DATA
  };
}

export function updateCurMethodData(updata) {
  return {
    type: DUBBO_UPDATE_CUR_METHOD_DATA,
    updata: updata,
    payload: true
  };
}

export async function fetchMethodData(method_id) {
  let result = await axios.get('/api/dubbomethod/get?id=' + method_id);
  return {
    type: DUBBO_FETCH_METHOD_DATA,
    payload: result
  }
}

export async function upMethodBaseData(data) {
  let {
    cname,
    mname,
    inname,
    outname,
    onlinedate,
    ctime,
    etime,
    status,
      cuserid,
      euserid,
      infid,
      _id
  } = data;
  // 过滤名称中有html标签存在的情况
  cname = htmlFilter(cname);
  const param = {
    cname,
    mname,
    inname,
    outname,
    onlinedate,
    ctime,
    etime,
    status,
    cuserid,
    euserid,
    infid,
    _id
  };
  return {
    type: DUBBO_UP_METHOD_DATA,
    payload: await axios.post('/api/interface_method/up', param)
  };
}

export async function fetchMethodBaseData(id) {
  let result = await axios.get('/api/dubbomethod/get?_id='+id);
  return {
    type: DUBBO_FETCH_METHODBASE_DATA,
    payload: result
  }
}

export async function changeMethodData(params) {
  let result = await axios.post('/api/dubbomethod/up', params);
  return {
    type: DUBBO_CHANGE_INTERFACE_METHOD_DATA,
    payload: result
  };
}

export async function changeEditStatus(status) {
  return {
    type: DUBBO_CHANGE_EDIT_STATUS,
    status: status
  };
}

export async function fetchMethodListMenu(projectId) {
  let result = await axios.get('/api/dubbomethod/list_menu?projectid=' + projectId);
  return {
    type: DUBBO_FETCH_METHOD_LIST_MENU,
    payload: result
  };
}

// 这个函数的功能是？
export async function fetchMethodList(params) {
  let result = await axios.get('/api/dubbomethod/list', { params });
  return {
    type: DUBBO_FETCH_METHOD_LIST,
    payload: result
  };
}

export async function fetchDubboInterfaceList(params) {
  let result = await axios.get('/api/dubbomethod/list_inf', { params });
  return {
    type: DUBBO_FETCH_DUBBO_INTERFACE_LIST,
    payload: result
  };
}

export async function deleteMethodData(id) {
  let result = await axios.post('/api/dubbomethod/del', { id: id });
  return {
    type: DUBBO_DELETE_METHOD_DATA,
    payload: result
  };
}

export async function deleteDubboInterfaceData(id) {
  let result = await axios.post('/api/dubbomethod/del_inf', { infid: id });
  return {
    type: DUBBO_DELETE_DUBBO_INTERFACE_DATA,
    payload: result
  };
}